import Logger from "../utils/Logger"

export default class SvgLoader {
  queue: any
  constructor(e) {
    this.queue = e
  }
  async load(e) {
    const log = new Logger("sdk.editIcon")
    const t = await this.queue.get(e, { responseType: "text" }),
      o = document.createElement("div")
    o.innerHTML = t
    const n = o.querySelector("svg")
    if (!n) throw Error("Failed trying to load " + e + "as an svg.")
    const r = n.getAttribute("width"),
      a = n.getAttribute("height")
    return (
      r || a
        ? (a && !r && (log.warn(e, "does not have a defined width. Defaulting width equal to height"), n.setAttribute("width", a)),
          r && !a && (log.warn(e, "does not have a defined height. Defaulting height equal to width"), n.setAttribute("height", r)))
        : (log.warn(e, "does not have a defined size. Defaulting to a 128x128 resolution"),
          n.setAttribute("width", SvgLoader.defaultResolution),
          n.setAttribute("height", SvgLoader.defaultResolution)),
      new Promise(e => {
        const t = new Image()
        t.onload = () => e(t)
        t.src = URL.createObjectURL(new Blob([n.outerHTML], { type: "image/svg+xml" }))
      })
    )
  }
  static defaultResolution: string
}
SvgLoader.defaultResolution = "128"
